package chapter15.exercise;

import java.util.HashMap;
import java.util.Map;

public class ClassTypeCapture<T> {
    private Class<T> kind;

    public ClassTypeCapture(Class<T> kind) {
        this.kind = kind;
    }

    private Map<String, Class<?>> map = new HashMap<>();

    public void addType(String typeName, Class<?> kind) {
        map.put(typeName, kind);
    }

    public Object createNew(String typeName) {
        Class<?> class1 = map.get(typeName);
        try {
            return class1.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public boolean f(Object obj) {
        return kind.isInstance(obj);
    }

    public static void main(String[] args) {
        ClassTypeCapture<Building> c = new ClassTypeCapture<>(Building.class);
        System.out.println(c.f(new Building()));
        System.out.println(c.f(new House()));
        ClassTypeCapture<House> d = new ClassTypeCapture<>(House.class);
        System.out.println(d.f(new Building()));
        System.out.println(d.f(new House()));
    }
}

class Building {
}

class House extends Building {
}
